// 修改$http的引用地址

const fs = require('fs')
const path = require('path')
const shell = require('shelljs'); //shelljs 更强大

const Src = path.join(__dirname, '../../../../src')

module.exports = function () {
    return new Promise((resolve) => {
        fs.access(Src + '/common/$http.js', fs.constants.F_OK, (err) => {
            if (!err) {
                // 清除main.js中的 ./common/$http.js
                let mainJs = fs.readFileSync(Src + '/main.js', 'utf8').replace(`// $http`, '').replace(`import './common/$http.js'`, '')
                fs.writeFileSync(Src + '/main.js', mainJs)

                // 改写permission.js中的 依赖
                let permissionJs = fs.readFileSync(Src + '/store/modules/permission.js', 'utf8')
                    .replace(`'@/common/permission/Axios'`, `'bui2/$bui/http/_Axios.js'`)
                fs.writeFileSync(Src + '/store/modules/permission.js', permissionJs)

                // 清除其他文件
                shell.rm('-rf', Src + '/common/$http.js');
                shell.rm('-rf', Src + '/common/permission/Axios.js');

                resolve()
            }
        })
        resolve()
    })
}
